package top.kaoshanji.p2ch19a;

/**
 * 代码清单-p2ch19a-535页a：ThreadLocal基本概念和用法
 * author kaoshanji
 * ^_^
 * create time 2025/2/28 10:30
 */
public class ThreadLocalBasic {

    static ThreadLocal<Integer> local = new ThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {

        // 子线程
        Thread child = new Thread() {

            @Override
            public void run() {
                System.out.println("child thread initial: " + local.get());
                local.set(200);
                System.out.println("child thread final: " + local.get());
            }
        };

        // 主线程
        local.set(100);

        child.start();
        child.join();

        // 主线程
        System.out.println("main thread final: " + local.get());

    }


}
